def readinp(fp):
    import re
    import GlobalParam as GP
    gp=GP.iparam
    ifilter=GP.InputSpecifier()
    while True:
        eatline=fp.readline()
        neat=len(eatline)
        if not eatline:
            break
        for i,if1 in enumerate(ifilter.if1gen()):
            f1=if1.match(eatline)
            if bool(f1):
                if i == 0:
                    pass
                elif i == 1:
                    # fid: flow identification
                    try:
                        fid=int(f1.group(1))
                    except AttributeError:
                        print("FLOW must be specified by number.")
                    else:
                        gp.set_flow(fid)
                elif i == 10:
                    # sid: stage identification
                    try:
                        sid=int(f1.group(1))
                    except AttributeError:
                        print("The stage must be specified by number.")
                    else:
                        # Current flow no.
                        fn=gp.get_nflow()-1
                        gp.set_stage_in_flow(fn,sid)
                elif i == 2:
                    pass
                elif i == 3:
                    # GENERIC_MODEL (deleted)
                    pass
                elif i == 4:
                    # GENERIC GROUP (deleted)
                    pass
                elif i == 5:
                    # PATH_MODEL
                    lasti=i
                elif i == 6:
                    # PATH GROUP
                    lasti=i
                elif i == 7:
                    # Stage property
                    try:
                        # flow id
                        fid=int(f1.group(1))
                    except AttributeError:
                        print("# of FLOW is missing in STAGE_PROPERTY.")
                    try:
                        # stage id
                        sid=int(f1.group(2))
                    except AttributeError:
                        print("# of stage is missing in STAGE_PROPERTY.")
                    # Conversion to array index from id
                    fn,sn=gp.id2index(fid,sid)
                    # Set stage property
                    while True:
                        # To escape loop when meet blank line
                        eatline=fp.readline().strip()
                        neat=len(eatline)
                        if neat == 0:
                            break
                        # 3 specifiers : DEFAULT/LINK/END
                        # Key words for each specifiers
                        # DEFAULT : NORMAL/END : PATH,GENERIC
                        # LINK : TARGET(n+n),PATH,GENERIC
                        # END : PATH,GENERIC
                        for j,if2 in enumerate(ifilter.if2gen()):
                            f2=if2.match(eatline)
                            if bool(f2):
                                if j==0:
                                    # DEFAULT:NORMAL,END
                                    if gp.af1(fn).get_nstage()-1 == sn:
                                        # Set as DEFAULT END
                                        gp.afs1(fn,sn).set_next(fn,sn,-1)
                                    else:
                                        # DEFAULT NORMAL
                                        gp.afs1(fn,sn).set_next(fn,sn+1,0)
                                    nstart=f2.end(1)+1
                                elif j==1:
                                    # LINK:TARGET(fid+sid),GENERIC,PATH
                                    rstr=f2.string[f2.end(1)+1:neat].strip()
                                    # Read n3+n4 target
                                    tr=re.match(r"\b(\d+)\s*[+]\s*(\d+)\b",rstr)
                                    fid2=int(tr.group(1))
                                    sid2=int(tr.group(2))
                                    fn2,sn2=gp.id2index(fid2,sid2)
                                    gp.afs1(fn,sn).set_next(fn2,sn2,1)
                                    nstart=tr.end(2)+1
                                elif j==2:
                                    # END
                                    gp.afs1(fn,sn).set_next(fn,sn,2)
                                    nstart=f2.end(1)+1
                                rstr=f2.string[nstart:neat].strip()
                                str1=re.split(r"\s+",rstr)
                                nword=len(str1)
                                if nword !=0 and nword%2 != 0:
                                    raise Exception
                                # Add path properties
                                for k in range(0,nword,2):
                                    # GENERIC or PATH
                                    tr1=re.search(r"\bG",str1[k])
                                    tr2=re.search(r"\bP",str1[k])
                                    tr_MODEL=re.search(r"\bM\D*(\d+)",str1[k+1])
                                    tr_GROUP=re.search(r"\bG\D*(\d+)",str1[k+1])
                                    # GENERIC (deleted)
                                    if bool(tr1):
                                        if bool(tr_MODEL):
                                            # MODEL
                                            pass
                                        elif bool(tr_GROUP):
                                            # GROUP
                                            pass
                                        else:
                                            raise Exception
                                    # PATH
                                    elif bool(tr2):
                                        if bool(tr_MODEL):
                                            # MODEL
                                            try:
                                                pid=int(tr_MODEL.group(1))
                                            except AttributeError:
                                                print("MODEL number is not specified in STAGE_PROPERTY")
                                            else:
                                                # Actual index
                                                pn=gp.afs1(fn,sn).get_npath()-1
                                                gp.afs1(fn,sn).set_model(pn,gp.id2pmodel(pid),3)
                                        elif bool(tr_GROUP):
                                            # GROUP
                                            try:
                                                # Group index
                                                pid=int(tr_GROUP.group(1))
                                            except AttributeError:
                                                print("GROUP number is not specified in STAGE_PROPERTY")
                                            else:
                                                pn=gp.afs1(fn,sn).get_npath()-1
                                                gp.afs1(fn,sn).set_model(pn,gp.getpgarr(pid-1),4)
                                        else:
                                            raise Exception
                                    else:
                                        raise Exception
                elif i == 8:
                    pass
                elif i == 9:
                    # Read the number of run
                    try:
                        n1=int(f1.group(1))
                    except AttributeError:
                        raise Exception("# of RUN is not specified.")
                    else:
                        gp.nrun=n1
                elif i == 11:
                    try:
                        mid=int(f1.group(1))
                        modelstr=f1.group(2)
                    except AttributeError:
                        print("MODEL number is missing.")
                    else:
                        if lasti == 3:
                            # Save file name (deleted)
                            # gp.set_generic_model(modelstr,mid)
                            pass
                        elif lasti == 5:
                            # Save file name
                            gp.set_path_model(modelstr,mid)
                        else:
                            raise Exception("Unspecified MODEL number occurs.")
                elif i == 12:
                    try:
                        gid=int(f1.group(1))
                    except AttributeError:
                        print("GROUP number is missing.")
                    rstr=f1.string[f1.end(1)+1:neat]
                    model=rstr.split()
                    nmodel=len(model)
                    m=[]
                    if lasti == 4:
                        for item in model:
                            pass
                    elif lasti == 6:
                        for item in model:
                            try:
                                pid=int(ifilter.ifilter4.match(item).group(1))
                            except AttributeError:
                                raise Exception("MODEL number in PATH_MODEL is missing.")
                            m.append(gp.id2pmodel(pid))
                        gp.set_path_group(m)
                    else:
                        raise Exception("MODEL specifier may locate in a wrong position.")
                elif i == 13:
                    pass
                elif i == 14:
                    try:
                        n1=int(f1.group(1))
                    except AttributeError:
                        print("SEED is not defined.")
                    else:
                        gp.seed=n1
                elif i == 15:
                    try:
                        n1=int(f1.group(1))
                    except AttributeError:
                        print("EXE is not defined.")
                    else:
                        gp.nexe=n1
                elif i == 16:
                    while True:
                        eatline=fp.readline().strip()
                        neat=len(eatline)
                        if neat == 0:
                            break
                        fout=re.match(r"\b(\d+)\s*-\s*(\d+)\s*-\s*(\d+)",eatline)
                        try:
                            fid=int(fout.group(1))
                            sid=int(fout.group(2))
                            rid=int(fout.group(3))
                        except AttributeError:
                            print("Insufficient argument in PRINT stagement.")
                        fn,sn=gp.id2index(fid,sid)
                        gp.set_nout(fn,sn,rid-1)
                else:
                    raise Exception("Undefined input specifier.")

